package dao;

import entity.ClassGrade;
import util.DruidUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ClassGradeDaoImpl implements ClassGradeDao {

    /**
     * 自定义成员芳芳实现班级增加的功能
     * @param classGrade
     * @return
     */
    @Override
    public int classGradeAdd(ClassGrade classGrade) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //1.获取连接
            connection = DruidUtils.getConnection();
            //2.准备Sql语句
            String sql = "insert into t_classGrade values(null,?,?,?,?,?)";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"className");
            preparedStatement.setString(2,"gradeName");
            preparedStatement.setString(3,"leaderName");
            preparedStatement.setString(4,"classSlogan");
            preparedStatement.setInt(5, Integer.parseInt("classPeoples"));
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection,preparedStatement);
        }
        return 0;//表示添加失败
    }

    @Override
    public int classGradeDelete(String[] classIds) {
        return 0;
    }

    /**
     * 自定义成员方法实现班级修改的功能
     * @param classGrade
     * @return
     */
    @Override
    public int classGradeModify(ClassGrade classGrade) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            //获取连接
            connection = DruidUtils.getConnection();
            //准备sql语句
            String sql = "update t_classGrade set className=?, gradeName=?, leaderName=?, classSlogan=?, classPeoples=? where classId=?";
            // 3.获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,classGrade.getClassName());
            preparedStatement.setString(2,classGrade.getGradeName());
            preparedStatement.setString(3,classGrade.getLeaderName());
            preparedStatement.setString(4,classGrade.getClassSlogan());
            preparedStatement.setInt(5,classGrade.getClassPeoples());
            return preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection,preparedStatement);
        }
        return 0;//表示添加失败
    }

    /**
     * 自定义成员放发实现根据编号来班级查找的功能
     * @param classId
     * @return
     */
    @Override
    public ClassGrade classGradeFind(int classId) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        ClassGrade classGrade = null;
        try {
            //获取连接
            connection = DruidUtils.getConnection();
            //准备插入数据的SQL语句
            String sql = "select * from t_classGrade where classId=?";
            //获取PreparedStatement并执行SQL语句
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,classId);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                classGrade = new ClassGrade(resultSet.getString("classId"),resultSet.getString("className"),resultSet.getString("leadeName"),resultSet.getString("classSlogan"),resultSet.getInt("classPeoples"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtils.close(connection,preparedStatement,resultSet);
        }
        return classGrade;
    }
}
